﻿using System.Data;
using Microsoft.SharePoint.Publishing;

namespace QueryDemo.ResultProviders
{
	/// <summary>
	/// CrossListQuery using similar functionality to the Content Query Web Part, including caching.
	/// </summary>
	public class CrossListQueryInfoResultProvider : BaseResultProvider
	{
		private CrossListQueryInfo _crossListQueryInfo;
		private CrossListQueryCache _crossListQueryCache;

		/// <summary>
		/// Sets properties related to use of the query within the web part.
		/// </summary>
		public CrossListQueryInfoResultProvider()
		{
			TypeOfQuery = QueryType.CrossListQueryInfo;
			QueryName = "CrossListQueryInfo";
		}

		public override bool CrossSiteCapable
		{
			get { return true; }
		}

		public override void Init()
		{
			_crossListQueryInfo = new CrossListQueryInfo
			                      	{
			                      		Webs = "<Webs Scope='SiteCollection'/>",
			                      		Lists = "<Lists ServerTemplate='" + ServerTemplate + "'/>",
			                      		ViewFields = GetViewFieldCaml(),
			                      		Query = CamlQuery,
			                      		RowLimit = (uint) RowLimit,
			                      		UseCache = true
			                      	};

			_crossListQueryCache = new CrossListQueryCache(_crossListQueryInfo);
		}

		public override DataTable ExecuteQuery()
		{
			DataTable resultsTable = _crossListQueryCache.GetSiteData(Site);
			return resultsTable;
		}
	}
}
